/*************************************************
 *FileName:      telephone.js
 *Description:   手机通讯录.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    /*
     * 变量初始化
     */
    var that =getUrlParam('that');
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var ohtml ='';
    var Cts;
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        var onav ='';
        for(var i=0;i<26;i++){
            onav += '<a data-id="'+String.fromCharCode(97+i)+'">'+String.fromCharCode(65+i)+'</a>';
        }
        $('.nav .top').after(onav);//字母输出
        
        $('.telephone').on('tap','.userphone',function(){
            $(this).toggleClass('active');
        })
        
        $('.telephone').on('tap','.ft-left .clickbox',function(){
            $(this).parent().toggleClass('active');
            $(this).parent().hasClass('active') ? $('.userphone').addClass('active') :$('.userphone').removeClass('active');
        })
        
        $('.mui-scroll-wrapper').on('tap',function(){
            $('input').blur()
        })
        $('.serch-input').focusin(function(){
            $('.title').css({display:'none'});
            if(!$(this).val()){ $('.userphone').css({display:'none'})}
            $('nav.nav').css({display:'none'});
        }).on('input',function(){
            $('.userphone').css({display:'none'})
            if($(this).val()){
                for(var i =0 ; i<$('.userphone').length ;i++){
                    otext =''+ $(this).val()+'';
                    Cts = $('.userphone').eq(i).find('.user').text()+$('.userphone').eq(i).find('.phone').text();
                    (Cts.indexOf(otext) > -1 )? $('.userphone').eq(i).css({display:'-webkit-flex'}) : false ;
                }
            }
        }).focusout(function(){
            if(!$(this).val()){
                $('.title').css({display:'block'});
                $('.userphone').css({display:'-webkit-flex'});
                $('nav.nav').css({display:'block'});
            }
        })
        
        var oheader =$('header.mui-bar').height()+$('.serch').height()+$('.title').height();
        $('.nav a').on('tap',function(){
            var obj ='#'+$(this).attr('data-id');
            var distance =-common.getTop($(obj)[0]);//计算高度
            var osum =distance+oheader;
            mui('.mui-scroll-wrapper').scroll().scrollTo(0,osum,100);
        })//字母跳转
        
        $('.nav .top').on('tap',function(){
            mui('.mui-scroll-wrapper').scroll().scrollTo(0,0,100);//100毫秒滚动到顶
        })//置顶
        $('.nav .bottom').on('tap',function(){
            mui('.mui-scroll-wrapper').scroll().scrollToBottom(0,0,100);//100毫秒滚动到顶
        })//置低
        
        $('.ft-right').on('tap',function(){
            common.loadingIn()
            var arr = {};
            for(var i=0; i<$('.main .active').length ; i++){
                var oname =$('.main .active').eq(i).find('.user').text();
                var ophone =$('.main .active').eq(i).find('.phone').text();
                arr[i]={'name':oname,'mobile':ophone};
            }
            postlist(arr);
        })
        
    }
    /**
     * find
     * 获取通讯录
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
// 监听plusready事件  
    document.addEventListener( "plusready", function(){
        show();
    }, false );
    function show(){
        plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_SIM, function (addressbook) {
            addressbook.find(["displayName","phoneNumbers"],function(contacts){
                var ophone ,oimg,ohtml='';
                for(var i=0 ;i<contacts.length; i++){
                    contacts[i].phoneNumbers == '' ? ophone=' ' :ophone =contacts[i].phoneNumbers[0].value;
                    ohtml+= '<a class="mui-table-view-cell userphone">'
                        +'<img src="images/user_phone.png" /><div class="mf-navigate-right ">'
                        +'<div class="user">'+contacts[i].displayName+'</div>'
                        +'<div class="phone">'+ophone+'</div></div>'
                        +'<i class="arrows clickbox"></i></a>'; 
                }
                $('.main').append(ohtml);
                rank();
            }, function () {
                alert("error");
            },{multiple:true});
        },function(e){
            alert("Get address book failed: " + e.message);
        });
    }
    /**r
     * rank
     * 排序
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function rank(){
        var wrap = $('.main'),othis;
        var html = 
                wrap.find('a')
                .map(function(){//获取文字
                    if($(this).hasClass('title')){
                        othis =[{
                            "name": $.trim($(this).attr('data-card')),
                            "id":$(this).attr('id'),
                            "text":$(this).text() ,
                        }] 
                    }else{
                        othis = [{ 
                            "name": $.trim($(this).find('.user').text()),
                            "text": $(this).html(),
                        }]
                    }
                    return othis
                })
                .toArray()//转换jq为数组
                .sort(function(a , b){
                  return a.name.localeCompare(b.name)//按照顺序排序
                })
                .map(function(txt){//拼接html
                    if(txt.id){
                        return '<a class="title" id="' + txt.id + '">'+txt.text+'</a>'
                    }else{
                        return '<li class="userphone">'+txt.text+'</li>';
                    }
                })
                .join('');
            wrap.html(html);//输出 
        mui('.mui-scroll-wrapper').scroll({
            indicators: false,      //是否显示滚动条 默认为true
            deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
            bounce: true           //是否启用回弹 默认true
        });
    }


//上传
function postlist(obj) {
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/shieldListAdd",
        dataType: 'jsonp',
        data: {
            uid: uid,
            token: token,
            list:obj,
        },
        success: function(data) {
            common.loadingOut();
            if(data.error_code == 0) {
                mui.toast('屏蔽成功!');
            } else if(data.error_code == 1001) {
                setTimeout(function(){
                        exitlogin();
                    },1000);
            } else {
                mui.toast(data.error_msg);
            }
        }
    });
}
